<#include "/myres/util.js" >

//百度统计
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?768c59656f477ee55313296aef326a78";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();

//百度站长自动推送
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https'){
   bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
  }
  else{
  bp.src = 'http://push.zhanzhang.baidu.com/push.js';
  }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
		

//删除全部重名的cookie
// tio.cookie.removeIfRepeat();
if (tio.cookie.removeIfRepeat()) {
    //location.reload();
}
//上传资源服务器，形如: http://192.168.1.168
var res_server = "${res_server}";

var curruser;
var curruserid;
var isSuper = false;
var ctx = "${api_ctx}";
var suffix = "${api_suf}";
var sessionName = '${session_cookie_name}';
var sessionValue = tio.cookie.getLast(sessionName);
log("sessionValue", sessionValue);
var tesft_tio_fdfdse = tio.cookie.getLast(tesoegEgac);



var pwd_key_login_aes='xOezYlYsPebzElys'; //登录时的密码key
var pwd_key_register_aes='iOezXlTsOebzElys'; //注册时的密码key

/**
 * 角色常量
 */
var roles = {
	adminNormal: 1,
	adminSuper: 99,
    normal: 2,
    uploadvideo:6,
    allow_read_doc:7,
    paid_doc:8
};


var isAdminNormal = false;     //是否是普通管理员，false: 不是普通管理员, true: 是普通管理员
var isNormal = false;          //是否是普通注册用户，false: 不是, true: 是
var isLogined = false;         //是否登录过了，false: 没有登录, true: 登录过了，匿名用户此值为false

/**
 * 获取真实的api请求路径
 * @param {*} url 
 */
function path(url) {
    var ret = "";
    if (url.indexOf("?") != -1) {
        var u = url.split("?");
        ret = ctx + u[0] + suffix + "?" + u[1];
    } else {
        ret = ctx + url + suffix;
    }
    return ret;
}

function layui_page_parseData (res) { //res 即为原始返回的数据
    return {
        "code": 0, //解析接口状态
        "msg": "", //解析提示文本
        "count": res.data.totalRow, //解析数据长度
        "data": res.data.list, //解析数据列表
        "pageSize": res.data.pageSize,
        "pageNumber": res.data.pageNumber
    };
}

/**
 * 给页面url增加pageNumber=1&pageSize=10
 * @param {*} pageNumber 
 * @param {*} pageSize 
 */
function layui_page_done(pageNumber, pageSize) {
    var newurl = window.location.href;
    var newurl = changeURLArg(newurl, 'pageNumber', pageNumber);
    var newurl = changeURLArg(newurl, 'pageSize', pageSize);
    var data = null;
    var title = null;
    history.pushState(data, title, newurl);
}

var layui_page_limits = [5, 10, 20, 40, 60, 100, 200, 300, 400, 500];


/**
 * 使用方法：
 * 1、res_url("/user/xxx.jpg");
 * 2、res_url(path);
 * @param {*} path 
 */
function res_url(path) {
    if (path) {
        var isAbPath = path.indexOf("http://");
        var isAbsPath = path.indexOf("https://");
        if (isAbPath >= 0 || isAbsPath >= 0) {
            return path;
        } else {
            if (path.indexOf("/") == 0) {
                return res_server + path;
            } else {
                return res_server + "/" + path;
            }
        }
    } else {
        // return res_server;
        return null;
    }
}

/**
 * 是否有指定的角色
 * @param {*} user 
 * @param {*} roleid 
 */
function hasRole(user, roleid) {
    if (!user) {
        return false;
    }
    return containRole(user.roles, roleid);
}

/**
 * 
 * @param {*} rolelist 
 * @param {*} roleid 
 */
function containRole(rolelist, roleid) {
    if (!rolelist || rolelist.length == 0) {
        return false;
    }
    for (var i = 0; i < rolelist.length; i++) {
        if (rolelist[i] == roleid) {
            return true;
        }
    }
    return false;
}

/**
 * 创建用户标识
 * @param {*} rolelist 
 */
function createVipFlag(rolelist) {
    var $vip = null;
    if (containRole(rolelist, roles.paid_doc)) {  //企业用户标识
        $vip = $('<i class="layui-badge fly-badge-vip vip_flag_' + roles.paid_doc + '" title="紫V：t-io文档付费用户标识">V</i>');
    } else if (containRole(rolelist, roles.allow_read_doc)) {  //文档阅读权限
        $vip = $('<i class="layui-badge fly-badge-vip vip_flag_' + roles.allow_read_doc + '" title="黄V：t-io文档阅读权限标识">V</i>');
    }
    return $vip;
}




/**
 * 判断某用户是不是普通管理员
 */
function _isAdminNormal(user) {
    return hasRole(user, roles.adminNormal);
}

 //获取当前用户
function curr() {
    ajax.get("/user/curr", {
        async: false,  //同步发送
        success: function (resp) {
            log(resp);
            if (resp.code == appcode.KICKTED) {
                notLogin(); //异常登录提示
            } else {
                if (resp.ok) {
                    var data = resp.data;
                    curruser = data;
                    traced_curruser = curruser;
                    curruserid = curruser.id;
                    isAdminNormal = _isAdminNormal(curruser);
                    isNormal = hasRole(curruser, roles.normal);
                    isSuper = hasRole(curruser, roles.adminSuper);

                    log("curruser:", curruser);
                    if (curruser.xx && curruser.xx == 1) {
                        isLogined = false;
                    } else {
                        isLogined = true;
                    }
                } else {
                    return;
                }
            }
        }
    });
}

if (sessionValue) {
    curr();
} else {
    log("没有session cookie");

}
// 以上 added by tanyaowu

//已成登录提示框
function notLogin() {
    // var html = '<div id="notLogin">' +
    //     '<h4>安全提示</h4>' +
    //     '<h6>您的账号在别处登录，当前账号已自动退出！</h6>' +
    //     '<button class="Beginlive againLogin">重新登录</button>' +
    //     '<button class="Beginlive cancel">关闭</button>' +
    //     '</div>';
    // $("body").html(html);
}

function laypage_render(id, data, pageCallback) {
    laypage.render({
        elem: id
        , count: data.totalRow //数据总数，从服务端得到
        , curr: data.pageNumber
        , limit: data.pageSize
        // , theme: '#1E9FFF'
        , jump: function (obj, first) {
            //obj包含了当前分页的所有参数，比如：pageNumber = obj.curr;  pageSize = obj.limit;
            // log(obj);
            //首次不执行
            if (!first) {
                pageCallback.call(pageCallback, obj.curr, obj.limit);
            }
        }
    });
}


$(function () {



});

//数值转换
function parseNum(num) {
    if (num <= 9999) {
        return num = num;
    } else {
        var num = (num / 10000).toFixed(1);
        return num + "万";
    }
}

/**
 * 时间转换
 * @param {*} date1 
 */
function parseDate(date1) {
    if (isBlank(date1)) {
        return "很久以前";
    }
    date1 = new Date(Date.parse(date1.replace(/-/g, "/"))).getTime()
    var date1 = new Date(date1);
    var date2 = new Date();
    var s1 = date1.getTime(), s2 = date2.getTime();
    var total = s2 - s1;

    var minutes = 1000 * 60;
    var hours = minutes * 60;
    var days = hours * 24;
    var years = days * 365;
    var m = Math.round(total / minutes);
    var shi = Math.round(m / 60);
    var tian = Math.round(shi / 24);
    // log(m)
    if (m >= 0 && m < 1) {
        return "刚刚";
    } else if (m >= 1 && m < 59) {
        return m + "分钟以前";
    } else if (m >= 60 && m < 1380) {
        return shi + "小时以前";
    } else if (m >= 1380 && m <= 10080) {
        return tian + "天以前";
    } else if (m > 10080) {
        return "很久以前"
    }
};
/**
 * 时间格式的处理
 * @param {*} second_time 时间戳（秒）
 */
function timeStamp(second_time) {
    var time = parseInt(second_time) + "秒";
    if (parseInt(second_time) > 60) {
        var second = parseInt(second_time) % 60;
        var min = parseInt(second_time / 60);
        time = min + "分" + second + "秒";
        if (min > 60) {
            min = parseInt(second_time / 60) % 60;
            var hour = parseInt(parseInt(second_time / 60) / 60);
            time = hour + "小时" + min + "分" + second + "秒";
            if (hour > 24) {
                hour = parseInt(parseInt(second_time / 60) / 60) % 24;
                var day = parseInt(parseInt(parseInt(second_time / 60) / 60) / 24);
                time = day + "天" + hour + "小时" + min + "分" + second + "秒";
            }
        }
    }
    if (parseInt(second_time) <= 0) {
        time = "0秒";
    }
    return time;
}


/**
 * 日期格式化
 * @param {*} time  时间戳
 * @param {*} fmt  格式：yyyy-MM-dd hh:mm:ss
 */
function formatDateByTime(time, fmt) {
    var date = new Date();
    date.setTime(time);
    var o = {
        "M+": date.getMonth() + 1,                 //月份 
        "d+": date.getDate(),                    //日 
        "h+": date.getHours(),                   //小时 
        "m+": date.getMinutes(),                 //分 
        "s+": date.getSeconds(),                 //秒 
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度 
        "S": date.getMilliseconds()             //毫秒 
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}

/**
 * 秒格式化为时分秒
 * @param {*} s  秒
 */
function formatDateByMilliseconds(s) {
    var t;
    if (s > -1) {
        var hour = Math.floor(s / 3600);
        var min = Math.floor(s / 60) % 60;
        var sec = s % 60;
        if (hour < 10) {
            t = '0' + hour + ":";
        } else {
            t = hour + ":";
        }

        if (min < 10) { t += "0"; }
        t += min + ":";
        if (sec < 10) { t += "0"; }
        t += sec;
    }
    return t;
}



<#include "/res/tio_js/tiows.js" >
